<?xml version="1.0" encoding="ASCII"?>
<!--  <<XSLT>> Monitoring data-to-XMI transformation rules -->
<!-- XSLT transformation rules to transform runtime monitoring data
    retieved from MASC middleware (i.e., Runtime monitoring database)
    to XMI representation of Stock Trading Web Services activiy diagram (UML model+policy assertions
    + runtime monitoring data).  It reads data from two files: XMI (UMl model+policy representation)
    and XML (runtime monitoring data). So both files should exist in the same directory
    where this XSLT file exist in order to successfully execute.
    Developed by Basem Suleiman & Vladimire Tosic. Copyrights NICTA -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:MASC_Prf="http:///schemas/MASC_Prf/_irUQYDRGEd2cWe87azLuXw/2"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML" version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="runTimeData" select="document('RunTimeData.xml')/RunTimeMonitoredData"/>
    <xsl:param name="GPA_BoolExp" select="//MASC_Prf:Goal_PA/@BooleanExpression"/>
    <xsl:param name="UPA_When" select="//MASC_Prf:Utility_PA/@When"/>
    <xsl:param name="APA_When" select="//MASC_Prf:Action_PA/@When"/>
    <xsl:param name="GPA_When" select="//MASC_Prf:Goal_PA/@When"/>

    <xsl:param name="UPA_baseElement" select="//MASC_Prf:Utility_PA/@base_Element"/>
    <xsl:param name="APA_baseElement" select="//MASC_Prf:Action_PA/@base_Element"/>
    <xsl:param name="GPA_baseElement" select="//MASC_Prf:Goal_PA/@base_Element"/>

    <!-- Copy the xmi:XMI node into output -->
    <xsl:template match="xmi:XMI">
        <xsl:copy>
            <xsl:apply-templates select="uml:Model"/>
        </xsl:copy>

    </xsl:template>

    <xsl:template match="uml:Model">

        <xsl:text>&#10;</xsl:text>
        <uml:Model>
            <xsl:for-each select="@*">
                <xsl:attribute name="{name()}">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates select="eAnnotations"/>
            <xsl:apply-templates select="packageImport"/>

            <packagedElement>
                <xsl:for-each select="packagedElement/@*">
                    <xsl:attribute name="{name()}">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </xsl:for-each>

                <xsl:text>&#10;</xsl:text>
                <packagedElement>
                    <xsl:for-each select="packagedElement/@*">
                        <xsl:attribute name="{name()}">
                            <xsl:value-of select="."/>
                        </xsl:attribute>
                    </xsl:for-each>

                    <xsl:for-each select="//packagedElement/node">
                        <xsl:choose>
                            <xsl:when
                                test="(@xmi:id =$UPA_baseElement) or (@xmi:id =$APA_baseElement) or (@xmi:id =$GPA_baseElement)">
                                <xsl:text>&#10;</xsl:text>
                                <xsl:copy-of select="."/>
                                <xsl:for-each select="$runTimeData/MonitoredData">
                                    <xsl:choose>
                                        <xsl:when test="ManagementInfoType/text() = 'Event'">

                                            <!--   CHECK MASCID = WHEN  YES DISPLAY -->
                                            <xsl:if
                                                test="( contains(MASCID/text(),$UPA_When)) or (contains(MASCID/text(), $APA_When)) or (contains(MASCID/text(),$GPA_When))">
                                                <xsl:text>&#10;</xsl:text>
                                                <ownedComment xmi:id="_9N63AD6nEd2-P860FSmnKg">
                                                  <xsl:text>&#10;</xsl:text>
                                                  <body>
                                                  <xsl:text>&#10;</xsl:text>
                                                  <xsl:copy-of select="."/>
                                                  <xsl:text>&#10;</xsl:text>
                                                  </body>
                                                  <xsl:text>&#10;</xsl:text>
                                                </ownedComment>
                                            </xsl:if>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <!-- CHECK MASCID = MONITORED COLLECTION OR PART OF BOOLEAN EXPRESSION                                             YES DISPLAY -->
                                            <xsl:if test="( contains (MASCID/text(), $GPA_BoolExp))">
                                                <xsl:text>&#10;</xsl:text>
                                                <ownedComment xmi:id="_9N63AD6nEd2-P860FSmnKg">
                                                  <xsl:text>&#10;</xsl:text>
                                                  <body>
                                                  <xsl:text>&#10;</xsl:text>
                                                  <xsl:copy-of select="."/>
                                                  <xsl:text>&#10;</xsl:text>
                                                  </body>
                                                  <xsl:text>&#10;</xsl:text>
                                                </ownedComment>
                                                <xsl:text>&#10;</xsl:text>
                                            </xsl:if>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>

                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>&#10;</xsl:text>
                                <xsl:copy-of select="."/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>

                    <xsl:for-each select="//packagedElement/edge">
                        <xsl:text>&#10;</xsl:text>
                        <xsl:copy-of select="."/>
                    </xsl:for-each>

                    <xsl:for-each select="//packagedElement/group">
                        <xsl:text>&#10;</xsl:text>
                        <xsl:copy-of select="."/>
                    </xsl:for-each>
                </packagedElement>
            </packagedElement>

            <xsl:for-each select="//profileApplication">
                <xsl:text>&#10;</xsl:text>
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </uml:Model>

        <xsl:for-each select="//MASC_Prf:Utility_PA">
            <xsl:text>&#10;</xsl:text>
            <xsl:copy-of select="."/>
        </xsl:for-each>

        <xsl:for-each select="//MASC_Prf:Goal_PA ">
            <xsl:text>&#10;</xsl:text>
            <xsl:copy-of select="."/>
        </xsl:for-each>

        <xsl:for-each select="//MASC_Prf:Action_PA ">
            <xsl:text>&#10;</xsl:text>
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="eAnnotations">
        <xsl:text>&#10;</xsl:text>
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="packageImport">
        <xsl:text>&#10;</xsl:text>
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>
